//
//  MoveCarSettingViewModel.m
//  carcareIOS
//
//  Created by 王然 on 16/9/19.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MoveCarSettingViewModel.h"
#import "carcareIOS-Swift.h"
@interface MoveCarSettingViewModel()

@property (nonatomic , copy) NSString * startTimeStr;

@property (nonatomic , copy) NSString * endTimeStr;

@property (nonatomic , strong) MoveCarManager * mgr;

@end
@implementation MoveCarSettingViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        __weak __typeof(self) wself = self;
        self.mgr = [MoveCarManager sharedInstance];
        RACSignal * modelValidSignal = RACObserve(self.mgr , setting);
        [modelValidSignal subscribeNext:^(MoveCarSettingModel * model) {
            if (model) {
                [((MoveCarSettingViewController *)wself.viewController).tableView reloadData];
            }else{
                [self.mgr updateNoBotherInfo];
            }
        }];
    }
    return self;
}
//点击设置勿扰模式开关
- (void)clickOnSwitch:(BOOL)isSwitchOn
{
    
    __weak __typeof(self) wself = self;
    [NetMoveCar setNoBotherInfoWithOpen:isSwitchOn start:wself.mgr.setting.startTime end:wself.mgr.setting.endTime success:^{
        wself.mgr.setting.flag = (isSwitchOn ? @1 : @0);
        wself.mgr.setting.startTime = ([wself.startTimeStr isNotNil] ? wself.startTimeStr : @"21:00");
        wself.mgr.setting.endTime = ([wself.endTimeStr isNotNil] ? wself.endTimeStr : @"07:00");
        [((MoveCarSettingViewController *)wself.viewController).tableView reloadData];
    } fail:^{
        [((MoveCarSettingViewController *)wself.viewController).tableView reloadData];
    }];
}
- (MoveCarSettingModel *)getDataModel
{
    return self.mgr.setting;
}
//点击设置勿扰模式时间
- (void)clickOnTimeCell:(MoveCarTimeCell *)cell
{
    __weak __typeof(self) wself = self;
    [((MoveCarSettingViewController *)self.viewController).timeV animationHidden:NO];
    [((MoveCarSettingViewController *)self.viewController).timeV reloadModel:self.mgr.setting click:^(NSString *startTime, NSString *endTime) {
        wself.startTimeStr = startTime;
        wself.endTimeStr = endTime;
        [wself clickOnSwitch:[wself.mgr.setting.flag boolValue]];
    }];
}

@end
